<?php
namespace app\usercp\controller;

use app\usercp\exception\UsercpException;
use think\Request;

class Custom extends Usercp
{

	public function initialize() {
        parent::initialize();
        $this->service = model('custom/custom', 'service');
    }

    //客户列表
    public function index(Request $request)
    {
    	if ($request->isAjax()){
    		$maps = [];
	    	$maps['parent_current_id'] = $this->custom['current_id'];
	    	if ($request->has('username','param',true)){
	    		$maps['username'] = $request->param('username');
	    	}
	    	if ($request->has('mobile','param',true)){
	    		$maps['mobile'] = $request->param('mobile');
	    	}
	    	if ($request->has('email','param',true)){
	    		$maps['email'] = $request->param('email');
	    	}
	    	$maps['status'] = 1;
	    	$list = $this->service->lists($maps,'create_time DESC',$this->page,$this->limit,true);
	    	if ($list->isEmpty()){
                return $this->result('',0,'暂无数据');
            }
	    	//注意：此处状态码一定和前端配置的状态码相同，否则数据会出问题(正常返回200，错误返回0)
	    	return $this->result($list,200);
    	}
    	return $this->fetch('index');
    }

    //添加客户
    public function add(Request $request)
    {
    	if ($request->isAjax()){
	        //验证
	        $validate = $this->validate($request->post(), 'app\custom\validate\Custom.add');
	        if (true !== $validate) {
	            return $this->result('',0,$validate);
	        }
    		$data = $request->post();
    		$data['parent_current_id'] = $this->custom['current_id'];
	    	$result = $this->service->create($data);
	    	if ($result === false){
	    		return $this->result('',0,$this->service->getError());
	    	}
	    	return $this->result($result,200,'添加成功');
    	}
    	return $this->fetch('add');
    }

    //编辑客户
    public function edit(Request $request)
    {
    	if ($request->isAjax()){
	        //验证
	        $validate = $this->validate($request->post(), 'app\custom\validate\Custom.edit');
	        if (true !== $validate) {
	            return $this->result('',0,$validate);
	        }
	    	$result = $this->service->save($request->post(),$request->post('current_id'));
	    	if ($result === false){
	    		return $this->result('',0,$this->service->getError());
	    	}
	    	return $this->result($result,200,'修改成功');
    	}
    	return $this->fetch('edit');
    }

    //客户充值
    public function recharge(Request $request)
    {
    	if ($request->isAjax()){
    		//验证
	        $validate = $this->validate($request->post(), 'app\custom\validate\Custom.recharge');
	        if (true !== $validate) {
	            return $this->result('',0,$validate);
	        }
	    	$result = $this->service->recharge($request->post('current_id'),$request->post('money'),$request->post('remark'),$this->custom['current_id']);
	    	if ($result === false){
	    		return $this->result('',0,$this->service->getError());
	    	}
	    	return $this->result($result,200,'充值成功');
	    	
    	}
    	return $this->fetch('recharge');
    }

    //客户扣费
    public function deduction(Request $request)
    {
    	if ($request->isAjax()){
    		//验证
	        $validate = $this->validate($request->post(), 'app\custom\validate\Custom.deduction');
	        if (true !== $validate) {
	            return $this->result('',0,$validate);
	        }
	    	$result = $this->service->deduction($request->post('current_id'),$request->post('money'),$request->post('remark'));
	    	if ($result === false){
	    		return $this->result('',0,$this->service->getError());
	    	}
	    	return $this->result($result,200,'扣费成功');
	    	
    	}
    	return $this->fetch('deduction');
    }

    //修改个人资料
    public function profile(Request $request)
    {
    	if ($request->isAjax()){
	        //验证
	        $validate = $this->validate($request->post(), 'app\custom\validate\Custom.edit');
	        if (true !== $validate) {
	            return $this->result('',0,$validate);
	        }
	    	$result = $this->service->save($request->post(),$this->custom['current_id']);
	    	if ($result === false){
	    		return $this->result('',0,$this->service->getError());
	    	}
	    	return $this->result($result,200,'修改成功');
    	}
    	return $this->fetch('profile');
    }

    //修改密码
    public function edit_password(Request $request)
    {
    	if ($request->isAjax()){
    		//验证
	        $validate = $this->validate($request->post(), 'app\custom\validate\Custom.edit_password');
	        if (true !== $validate) {
	            return $this->result('',0,$validate);
	        }
	    	$result = $this->service->edit_password($this->custom['current_id'],$request->post('oldpassword'),$request->post('password'));
	    	if ($result === false){
	    		return $this->result('',0,$this->service->getError());
	    	}
	    	return $this->result($result,200,'修改成功');
    	}
    	return $this->fetch('edit_password');
    }
}